<template>
  <div>
    <FormPanel
        ref="formPanel"
        :list="formList"
        :cols="1"
        :labelWidth="110"
        formType="add"
        :isSubmitBtn="false"
        @formChange="formChangeHandle"
      />
      <div
        :style="{
          position: 'absolute',
          left: 0,
          bottom: 0,
          right: 0,
          zIndex: 9,
          borderTop: '1px solid #e9e9e9',
          padding: '10px 20px',
          background: '#fff',
          textAlign: 'center'
        }"
      >
        <el-button @click="closeDialog">取 消</el-button>
        <el-button @click="saveDialog" type="primary" :loading="addBtnState">确 定</el-button>
      </div>
      <BaseDialog
      :visible.sync="showAddNew"
      title="创建角色结果"
      destroyOnClose
      width="20%"
      :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto'}">
        <div >
            <h3 style="text-align: center;font-weight: 700;font-size: 18px;line-height: 50px;">创建角色成功</h3>
            <p style="margin-left: 40px;font-size: 15px;font-weight: 700;line-height: 40px;">角色代码：<span style="color:#ccc;">{{roleNum}}</span></p>
            <p style="margin-left: 40px;font-size: 15px;font-weight: 700;line-height: 40px;">角色名称：<span style="color:#ccc;">{{roleName}}</span></p>
<!--            <p style="margin-left: 40px;font-size: 15px;font-weight: 700;line-height: 40px;">角色类型：<span style="color:#ccc;">{{roleType}}</span></p>-->
            <div style="text-align: center;margin-top: 30px;">
                <el-button @click="closeDialog('refresh')">返回列表</el-button>
                <el-button @click="deploy" type="primary">配置角色权限</el-button>
            </div>
        </div>
    </BaseDialog>
  </div>
</template>

<script>
import { mapState } from 'vuex';
import { dictionary } from '@/utils/dictMixin';
import { addRole } from '@/api/common/auth/roleManage';
import * as types from '@/api/common/auth/roleManage/types';
import { IDNumber, phone,endTime, email, formatDates, checkPassport,testText,systemCode } from '@/utils/validate'

export default {
  name: 'Rolemanageaddform',
  mixins: [dictionary],
  props: {
    type: {
      type: String,
      default: 'add'
    },
    rowData: {
      type: Object,
      default: () => ({})
    },
    dataType: {
      type: Number,
    }
  },
  data() {
    this.saveData = null;
    return {
      formList: this.createFormList(),
      roleNum:"",
      roleName:"",
      roleType:"",
      showAddNew:false,
      deployData:{},
      roleId:""
    };
  },
  computed: {
    ...mapState('app', ['btnLoading']),
    addBtnState() {
      this.roleNum=this.formList[0].initialValue
      this.roleName=this.formList[1].initialValue
      this.roleType=this.formList[2].initialValue
      return this.btnLoading[types.YAN_ROLE_ADD];
    }
  },
  created() {
    if (this.type === 'edit') {
      this.formList.forEach(x => {
        x.initialValue = this.rowData[x.fieldName];
      });
    }
    this.getCookie()
  },
  methods: {
    createFormList() {
      return [
        {
          type: 'INPUT',
          label: '角色代码',
          fieldName: 'roleCode',
          placeholder: '请输入...',
          style: { width: '200px' },
          maxlength:30,
          rules: [{ required: true, trigger: 'blur',validator:systemCode }]
        },
        {
          type: 'INPUT',
          label: '角色名称',
          fieldName: 'roleName',
          placeholder: '请输入...',
          style: { width: '200px' },
          // ,validator:testText
          maxlength:32,
          rules: [{ required: true,  trigger: 'blur',message: '请输入角色名称' }]
        },
        {
          type: 'SELECT',
          label: '角色类型',
          fieldName: 'roleType',
          placeholder: '请输入...',
          style: { width: '200px' },
          itemList: this.createDictList('1007'),
          initialValue:'',
          disabled:false,
          rules: [{ required: true,  trigger: 'blur',message: '请输入' }]
        }
      ]
    },
    formChangeHandle(val) {
      this.saveData = val;
    },
    // getCookie(){
    //     let allCookie=document.cookie.split(';')
    //     allCookie.forEach(x=>{
    //       if(x.indexOf('dataType') > -1 ){
    //         this.dataType=x.split('=')[1]
    //         }
    //     })
    //     //判断是否为车厂10461003
    //     console.log(this.dataType,"dataType")
    //     if(this.dataType == 10461003){
    //       console.log(this.dataType,"车厂")
    //     }
    //   },
    async saveDialog() {
      /*20190903注释---替换为 this.saveData={}
      this.$refs.formPanel.SUBMIT_FORM();
      this.$set(this.saveData, 'dataType', '10461001')
      this.$set(this.saveData, 'dataSource', '10451001') */
      let formPanelData = this.$refs.formPanel.SUBMIT_FORM();
      console.log(formPanelData,"formPanelData")
      this.saveData={
        'dataType':this.dataType,
        'dataSource':this.dataType == 10461003?10451003:this.dataType == 10461001?10451002:'',
        // 'roleCode':this.formList[0].initialValue,
        // 'roleName':this.formList[1].initialValue,
        ...formPanelData
      }
      if (formPanelData) {
        // var g = /^[\u4E00-\u9FA5]{0,32}$/;
        // var g1 = g = /^([0-9]){1,16}$/
        // var g2 = /^([0-9_a-zA-Z]).{1,16}$/
        // var g3 = /^[A-Za-z]{1,16}/
        // if(g.test(this.saveData.roleName)){
          // if(g1.test(this.saveData.roleCode) ||g2.test(this.saveData.roleCode) ||g3.test(this.saveData.roleCode)){
              const res = await addRole(this.saveData);
              console.log(res,"res")
              this.roleId=res.data.roleId
              if (res.resultCode === 200) {
                this.$notify.success({title: '提示信息',message:'新增成功!'});
                this.showAddNew=true
                // 重新加载
                // this.closeDialog('refresh');
              }else {
                this.$notify.error({title: '提示信息',message:res.errMsg||'新增失败!'});
                // this.closeDialog()
              }
          // }
        // }
      }
    },
    // 配置权限
    deploy(){
      console.log("配置权限")
      this.deployData={
        roleCode:this.roleNum,
        roleName:this.roleName,
        roleType:this.roleType,
        roleId:this.roleId
      }
      this.$emit('showDrawer', true,this.deployData);
      this.closeDialog()
    },
    closeDialog(type) {
      this.$emit('close', false, type);
    }
  }
};
</script>

<style lang="less" scoped>
</style>
